<template>
  <!-- 一种用于具有镜面高光的光泽表面的材质。
该材质使用非物理的Blinn-Phong模型来计算反射率。 与MeshLambertMaterial中使用的Lambertian模型不同，该材质可以模拟具有镜面高光的光泽表面（例如涂漆木材）。 -->
  <div class="mesh-phong">
    <div id="scene"></div>
    <div class="controls-box">
      <section>
        <el-row>
          <el-checkbox v-model="properties.visible">visible</el-checkbox>
        </el-row>
        <el-row>
          <el-checkbox v-model="properties.transparent"
            >transparent</el-checkbox
          >
        </el-row>
        <el-row>
          <el-col :span="8" class="label-col"><label> color</label></el-col>
          <el-col :span="16">
            <div class="color-select-layer">
              <el-input :value="properties.color"></el-input>
              <div>
                <el-color-picker v-model="properties.color"></el-color-picker>
              </div>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8" class="label-col"><label> emissive</label></el-col>
          <el-col :span="16">
            <div class="color-select-layer">
              <el-input :value="properties.emissiveColor"></el-input>
              <div>
                <el-color-picker
                  v-model="properties.emissiveColor"
                ></el-color-picker>
              </div>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8" class="label-col"><label> specular</label></el-col>
          <el-col :span="16">
            <div class="color-select-layer">
              <el-input :value="properties.specularColor"></el-input>
              <div>
                <el-color-picker
                  v-model="properties.specularColor"
                ></el-color-picker>
              </div>
            </div>
          </el-col>
        </el-row>

        <el-row>
          <div v-for="(item, key) in properties" :key="key">
            <div v-if="item && item.name != undefined">
              <el-col :span="8">
                <span class="vertice-span">{{ item.name }}</span>
              </el-col>
              <el-col :span="13">
                <el-slider
                  v-model="item.value"
                  :min="item.min"
                  :max="item.max"
                  :step="item.step"
                ></el-slider>
              </el-col>
              <el-col :span="3">
                <span class="vertice-span">{{ item.value }}</span>
              </el-col>
            </div>
          </div>
        </el-row>
        <el-row>
          <el-col :span="8" class="label-col"><label>side</label></el-col>
          <el-col :span="16">
            <el-select v-model="properties.side" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8" class="label-col"><label>mesh</label></el-col>
          <el-col :span="16">
            <el-select v-model="properties.selectMesh" placeholder="请选择">
              <el-option
                v-for="item in selectMeshOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-col>
        </el-row>
      </section>
    </div>
  </div>
</template>

<script>
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";

let scene;
let camera;
let renderer;
let controls;
export default {
  data() {
    return {
      options: [
        {
          value: "front",
          label: "front",
        },
        {
          value: "back",
          label: "back",
        },
        {
          value: "double",
          label: "double",
        },
      ],

      selectMeshOptions: [
        {
          value: "cube",
          label: "cube",
        },
        {
          value: "sphere",
          label: "sphere",
        },
        {
          value: "plane",
          label: "plane",
        },
      ],

      properties: {
        opacity: {
          name: "opacity",
          value: 0.3,
          min: 0,
          max: 1,
          step: 0.1,
        },
        shininess: {
          name: "shininess",
          value: 30,
          min: 0,
          max: 300,
          step: 1,
        },
        color: "#EB193D",
        emissiveColor: "#AB193D",
        specularColor: "#111111",
        selectMesh: "cube",
        side: "front",
        transparent: false,
        visible: true,
      },
      cube: null,
      sphere: null,
      plane: null,
      activeMesh: null,
      meshMaterial: null,
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    // 初始化
    init() {
      this.createScene(); // 创建场景
      this.createMesh(); // 创建网格模型
      this.createLight(); // 创建光源
      this.createCamera(); // 创建相机
      this.createRender(); // 创建渲染器
      this.createControls(); // 创建控件对象
      this.render(); // 渲染
    },
    // 创建场景
    createScene() {
      scene = new THREE.Scene();
    },
    // 创建网格模型
    createMesh() {
      const geometry = new THREE.PlaneGeometry(100, 100, 4, 4); //创建一个平面对象
      const planeMaterial = new THREE.MeshPhongMaterial({
        color: 0x7777ff,
      });
      const plane = new THREE.Mesh(geometry, planeMaterial);
      plane.receiveShadow = true;

      //设置平面位置
      plane.rotation.x = -0.5 * Math.PI;
      plane.position.set(0, -20, 0);
      scene.add(plane);

      const sphereGeometry = new THREE.SphereGeometry(14, 20, 20);
      const cubeGeometry = new THREE.BoxGeometry(15, 15, 15);
      const planeGeometry = new THREE.PlaneGeometry(14, 14, 4, 4);

      //创建材质
      this.meshMaterial = new THREE.MeshPhongMaterial({
        color: 0x7777ff,
      });

      //球、立方体、平面
      this.sphere = new THREE.Mesh(sphereGeometry, this.meshMaterial);
      this.cube = new THREE.Mesh(cubeGeometry, this.meshMaterial);
      this.plane = new THREE.Mesh(planeGeometry, this.meshMaterial);

      this.sphere.position.set(-12, 3, 2);
      this.cube.position = this.sphere.position;
      this.plane.position = this.sphere.position;
      this.activeMesh = this.sphere;
    },
    // 创建光源
    createLight() {
      // 环境光
      const ambientLight = new THREE.AmbientLight(0xab193d, 0.1); // 创建环境光
      scene.add(ambientLight); // 将环境光添加到场景

      const pointLight = new THREE.PointLight(0xffffff);
      //设置点光源照射距离为100
      //这个距离表示从光源到光照强度为0的位置。 当设置为0时，光永远不会消失(距离无穷大)。
      pointLight.distance = 100;
      scene.add(pointLight);
    },
    // 创建相机
    createCamera() {
      const element = document.getElementById("scene");
      const k = element.clientWidth / element.clientHeight; // 窗口宽高比
      // PerspectiveCamera( fov, aspect, near, far )
      camera = new THREE.PerspectiveCamera(35, k, 0.1, 1000);
      camera.position.set(-60, 60, 100); // 设置相机位置

      camera.lookAt(new THREE.Vector3(10, 0, 0)); // 设置相机方向
      scene.add(camera);
    },
    // 创建渲染器
    createRender() {
      const element = document.getElementById("scene");
      renderer = new THREE.WebGLRenderer();
      renderer.setSize(element.clientWidth, element.clientHeight); // 设置渲染区域尺寸
      renderer.shadowMap.enabled = true; // 显示阴影
      renderer.shadowMap.type = THREE.PCFSoftShadowMap;
      renderer.setClearColor(0x3f3f3f, 1); // 设置背景颜色
      element.appendChild(renderer.domElement);
    },
    updateMesh(selectMesh) {
      scene.remove(this.activeMesh);
      switch (selectMesh) {
        case "cube":
          this.activeMesh = this.cube;
          break;
        case "sphere":
          this.activeMesh = this.sphere;
          break;
        case "plane":
          this.activeMesh = this.plane;
          break;
      }
      scene.add(this.activeMesh);
    },
    updateSide(side) {
      switch (side) {
        case "front":
          this.meshMaterial.side = THREE.FrontSide;
          break;
        case "back":
          this.meshMaterial.side = THREE.BackSide;
          break;
        case "double":
          this.meshMaterial.side = THREE.DoubleSide;
          break;
      }
      this.meshMaterial.needsUpdate = true;
    },
    // 更新属性
    updateFun() {
      this.meshMaterial.transparent = this.properties.transparent;
      this.meshMaterial.opacity = this.properties.opacity.value;
      //材质的颜色
      this.meshMaterial.color.setStyle(this.properties.color);
      //这是该材质的发射的颜色。它其实并不像一个光源，只是一种纯粹的、不受其它光照影响的颜色。默认值为黑色
      this.meshMaterial.emissive.setStyle(this.properties.emissiveColor);
      //该属性指定该材质的光亮程度及高光部分颜色，如果设置成和color属性相同的颜色，将得到一种更加类似金属的材质。如果设置成灰色（grey），材质变得更像塑料
      this.meshMaterial.specular = new THREE.Color(
        this.properties.specularColor
      );
      //高亮的程度，越高的值越闪亮。默认值为 30。
      this.meshMaterial.shininess = this.properties.shininess.value;
      this.meshMaterial.visible = this.properties.visible;

      this.updateMesh(this.properties.selectMesh);
      this.updateSide(this.properties.side);
      this.meshMaterial.needsUpdate = true;
      this.activeMesh.rotation.y += 0.01; // 旋转
    },

    render() {
      this.updateFun();
      renderer.render(scene, camera);
      requestAnimationFrame(this.render);
    },
    // 创建控件对象
    createControls() {
      controls = new OrbitControls(camera, renderer.domElement);
    },
  },
};
</script>

<style>
.mesh-phong {
  width: 100%;
  height: 100%;
  position: relative;
}
#scene {
  width: 100%;
  height: 100%;
}
.controls-box {
  position: absolute;
  right: 5px;
  top: 5px;
  width: 300px;
  padding: 10px;
  background-color: #fff;
  border: 1px solid #c3c3c3;
}
.label-col {
  padding: 8px 5px;
}
.color-select-layer {
  display: flex;
}
.vertice-span {
  line-height: 38px;
  padding: 0 2px 0 10px;
}
</style>

